package com.nd.mobile.study.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.constants.FlagConstants;
import com.nd.mobile.study.common.exception.LocalErrorException;
import com.nd.mobile.study.domain.User;

public class MoreActivity extends BaseActivity {
	
	private RelativeLayout mLoginInfo;
//    private RelativeLayout mAdvise;
    private RelativeLayout mCheckUpdate;
    private RelativeLayout mAbout;
    private RelativeLayout mExit;
    
    private TextView mLoginInfoTv;
    
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.more);
	}
	
	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
	}
	
	@Override
	protected void initViews() {
		super.initHeader();
		mHeaderTitleTv.setText(R.string.txt_title_more);
		mHeaderBtnBack.setVisibility(View.GONE);
		
		mLoginInfo = (RelativeLayout) findViewById(R.id.more_login_info);
//    	mAdvise = (RelativeLayout) findViewById(R.id.more_advise);
    	mCheckUpdate = (RelativeLayout) findViewById(R.id.more_checkupdate);
    	mAbout = (RelativeLayout) findViewById(R.id.more_about);
    	mExit = (RelativeLayout) findViewById(R.id.more_exit);
    	
//    	mAdvise.setOnClickListener(mListItemOnClickListener);
    	mCheckUpdate.setOnClickListener(mListItemOnClickListener);
    	mAbout.setOnClickListener(mListItemOnClickListener);
    	mExit.setOnClickListener(mListItemOnClickListener);
    	
    	//折腾下login的文字
    	mLoginInfoTv = (TextView)findViewById(R.id.tv_login_info);
    	if(mApp.hasUserLogin() == false){
    		mHeaderBtnLogin.setVisibility(View.GONE);
    		mLoginInfoTv.setText("点此登录");
    		mLoginInfo.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(MoreActivity.this,LoginActivity.class);
					startActivity(intent);
				}
			});
    	}else{
    		try {
				mLoginInfoTv.setText("已登录："+mApp.getUser().getRealName()+"----点此注销");
			} catch (LocalErrorException e) {
				e.printStackTrace();
			}
    		mLoginInfo.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					MoreActivity.this.mApp.setUser(null);
					User.clearUserInfoSharedPreferences(MoreActivity.this.mApp);
					Intent intent = new Intent(MoreActivity.this,LoginActivity.class);
					intent.putExtra(FlagConstants.LOGIN_FLAG_FROM_MORE, true);
					MoreActivity.this.finish();
					startActivity(intent);
				}
			});
    	}
	}
	
    
	private OnClickListener mListItemOnClickListener = new OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		switch (v.getId()) {
/*    		case R.id.more_advise:
    			//记录到友盟--建议
    			break;*/
    		case R.id.more_checkupdate:
    			//记录到友盟--检查更新
//    			checkUpdate();
    			break;
    		case R.id.more_about:
    			//记录到友盟--关于
    			startActivity(new Intent(MoreActivity.this, AboutActivity.class));
    			break;
    		case R.id.more_exit:
    			//记录到友盟--退出
    			showExitDialog();
    			break;
    		default:
    			break;
    		}
    	}
    };
    
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = (keyCode == KeyEvent.KEYCODE_BACK);
        if (flag) {
            showExitDialog();
        }
        return flag;
	}
}